package david.java.flink_sql.udf;

import org.apache.flink.table.functions.ScalarFunction;

import java.util.Arrays;
import java.util.HashSet;

/**
 * @Description: split string into array by the specified separator, and select the Nth element.
 *               The index of head element is 0, and the tail is -1
 * @Author: David
 * @Date: Create in 4:32 下午 2022/1/19
 */
public class MergeString extends ScalarFunction {


    public String eval(String separator, String str1, String str2) {
        if (str1 == null && str2 == null) {
            return null;
        }

        if (str1 == null) {
            return str2;
        }

        if (str2 == null) {
            return str1;
        }

        HashSet<String> set = new HashSet<>();

        String[] split1 = str1.split(separator);
        String[] split2 = str2.split(separator);

        set.addAll(Arrays.asList(split1));
        set.addAll(Arrays.asList(split2));


        return String.join(separator, set);
    }

    public static void main(String[] args) {
        MergeString merge = new MergeString();
        System.out.println(merge.eval(",", "1,1,2,3", "a,v,g,h"));
    }
}
